package com.workcell.workdata.xthings.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.workcell.workdata.xthings.constant.HardwareStateEnum;
import com.workcell.workdata.xthings.dto.BaseVo;
import com.workcell.workdata.xthings.entity.BaseTemperatureHumidity;
import com.workcell.workdata.xthings.mapper.BaseTemperatureHumidityMapper;
import com.workcell.workdata.xthings.service.BaseTemperatureHumidityService;
import org.springframework.stereotype.Service;

/**
 * 温湿度计型号
 *
 * @author guoyu
 * @date 2023-04-15 14:02:16
 */
@Service
public class BaseTemperatureHumidityServiceImpl extends ServiceImpl<BaseTemperatureHumidityMapper, BaseTemperatureHumidity> implements BaseTemperatureHumidityService {

	/**
	 * 模糊查询分页
	 * @param baseVo
	 * @return
	 */
	@Override
	public Page<BaseTemperatureHumidity> vaguePage(BaseVo baseVo) {
		LambdaQueryWrapper<BaseTemperatureHumidity> baseCondition = new QueryWrapper<BaseTemperatureHumidity>().lambda()
				.and(ObjectUtil.isNotEmpty(baseVo.getKeyWord()), i -> i
						.like(BaseTemperatureHumidity::getName,baseVo.getKeyWord())
						.or()
						.like(BaseTemperatureHumidity::getManufacturer,baseVo.getKeyWord()))
				.eq(ObjectUtil.isNotEmpty(baseVo.getState()),BaseTemperatureHumidity::getState,baseVo.getState())
				.and(ObjectUtil.isNotEmpty(baseVo.getStartTime()) || ObjectUtil.isNotEmpty(baseVo.getEndTime())
						, i -> i.between(BaseTemperatureHumidity::getCreateTime,baseVo.getStartTime(),baseVo.getEndTime()));
		Page<BaseTemperatureHumidity> page = new Page<>(baseVo.getPageNum(), baseVo.getPageSize());
		page.addOrder(OrderItem.desc("create_time"));
		return this.page(page,baseCondition);
	}

	/**
	 * 修改温湿度计型号基本信息
	 * @param baseTemperatureHumidity
	 * @return
	 */
	@Override
	public Boolean updateBaseTvBox(BaseTemperatureHumidity baseTemperatureHumidity) {
		Assert.isTrue(ObjectUtil.isNotEmpty(baseTemperatureHumidity.getId()),"ID不能为空");
		//basePowermeter.setState(null);
		// 查询该流量计的状态
		BaseTemperatureHumidity baseTemperatureHumidityOld = this.getById(baseTemperatureHumidity.getId());
		Assert.isTrue(ObjectUtil.isNotEmpty(baseTemperatureHumidityOld),"无该条记录，请核实您的操作");
		Assert.isTrue(HardwareStateEnum.DRAF.getCode().equals(baseTemperatureHumidityOld.getState()),"状态为草稿才能修改");
		return this.updateById(baseTemperatureHumidity);
	}

	/**
	 * 修改状态
	 * @param baseTemperatureHumidity
	 * @return
	 */
	@Override
	public Boolean updateStateById(BaseTemperatureHumidity baseTemperatureHumidity) {
		Assert.isTrue(ObjectUtil.isNotEmpty(baseTemperatureHumidity.getId()),"ID不能为空");
		Assert.isTrue(ObjectUtil.isNotEmpty(baseTemperatureHumidity.getState()),"状态不能为空");
		// 查询该温湿度计的状态
		BaseTemperatureHumidity baseTemperatureHumidityOld = this.getById(baseTemperatureHumidity.getId());
		Assert.isTrue(ObjectUtil.isNotEmpty(baseTemperatureHumidityOld),"无该条记录，请核实您的操作");
		if (HardwareStateEnum.INUSE.getCode().equals(baseTemperatureHumidityOld.getState())
				|| HardwareStateEnum.SUSPEND.getCode().equals(baseTemperatureHumidityOld.getState())){
			if (HardwareStateEnum.DRAF.getCode().equals(baseTemperatureHumidity.getState())){
				Assert.isTrue(false,"当状态为使用中或暂停中时，不能修改为草稿状态");
			}
		}
		return this.updateById(baseTemperatureHumidity);
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@Override
	public Boolean delById(Long id) {
		Assert.isTrue(ObjectUtil.isNotEmpty(id),"ID不能为空");
		// 查询该温湿度计的状态
		BaseTemperatureHumidity baseTemperatureHumidityOld = this.getById(id);
		Assert.isTrue(ObjectUtil.isNotEmpty(baseTemperatureHumidityOld),"无该条记录，请核实您的操作");
		if (HardwareStateEnum.INUSE.getCode().equals(baseTemperatureHumidityOld.getState())
				|| HardwareStateEnum.SUSPEND.getCode().equals(baseTemperatureHumidityOld.getState())){
			Assert.isTrue(false,"当状态为使用中或暂停中时，不能删除该条记录");
		}
		// 删除记录
		return this.removeById(id);
	}
}
